<?php
namespace Index\Controller;
use Think\Controller;
class AskController extends CommonController {
	public function indexAction(){
		layout('layoutask');//引用精简模板layoutask

       	$this->display();
    }

    public function howtoaskAction(){


       	$this->display();
    }

    public function saveAction()
    {
        // 把数据接收

        // 插入数据库，返回问题ID

        // 再根据ID循环插入多个分类
    	if(IS_POST){
            $category = I('post.category');
            $title = I('post.title');
            $content = I('post.editorValue');
            $create_time = time();
            $uid = $_SESSION['id'];

            $sql = "insert into question (title,content,create_time,uid) value('{$title}','{$content}','{$create_time}','{$uid}')";
            $res = M()->execute($sql);

            if ($res) {
                $qid = M()->getLastInsID();//得到插入的最后一个数据的id
                //循环category分类信息，将它们按对应的cid，qid插入到category_question分类问题连接的中间表中，提问成功后，跳转到对应问题页面，给对应问题的跳转页面传一个参数，区分不同问题，将$qid设置成它的参数，推到Question控制器下的index方法处理，那边用get接参数$id
                foreach ($category as $cid) {
                    $sql = "insert into category_question (cid,qid) value('{$cid}','{$qid}')";
                    $res =  M()->execute($sql);                    
                }
                $this->success('提问成功',U('Index/question/index',array('id'=>$qid)));
            }else{
                $this->error('提问失败');
            }
            
    		

    		
    	}
    }


    public function ueditorAction(){
        $data = new \Org\Util\Ueditor();
        echo $data->output();
    }
}